#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2020/4/17 9:21

@author: aimee.mei
"""

import requests
import pandas as pd
from base.get_config import get_environment
import base.globalvars as glo
from util.db_util import execute_query
from proj_spec.inews.user_admin import get_user_id, get_user_pay


def get_urls_restriction(restrict_user_count=0):
    sql = "SELECT url, pay_count, not_pay_count FROM nlp_news_url_count WHERE restrict_user_count=%d and url = '/webui/product/region/detail'" % restrict_user_count
    result = execute_query(sql)
    result_dfm = pd.DataFrame(list(result))
    result_dfm.columns = ['url', 'pay_count', 'not_pay_count']
    return result_dfm


def reload_cache(username, path):
    root_url = glo.get_value("url1")
    list_url = "/webui/reload/reloadCache"
    url = root_url + list_url
    redis_path = path.replace('/', '_')
    pay = get_user_pay('username')
    if pay == 2:
        redis_pay = 'pay'
    elif pay == 1:
        redis_pay = 'not_pay'
    data = {"cacheKeys": "%s:webui:%s:%s:%d" % (
        get_environment(), redis_pay, redis_path, get_user_id(username))}
    # data = {"cacheKeys": "%s:webui:not_pay:_webui_com_portrayal:%s"%(get_environment(),get_user_id("automation_tester1"))}
    session = requests.Session()
    response = session.post(url, params=data)
    assert response.status_code == 200, "返回结果code不为200"


def user_setCanUse(username, count):
    root_url = glo.get_value("url2")
    list_url = "/user/setCanUse"
    url = root_url + list_url
    data = {"uid": get_user_id(username), "count": count}
    r = requests.post(url, data=data)
    r_json = r.json()
    assert r.status_code == 200, "返回结果code不为200"
